package net.easyjava.api.controller;

import net.easyjava.api.constants.WebConstants;
import net.easyjava.base.manager.UserManager;
import net.easyjava.tools.BaseController;
import net.easyjava.tools.log.annotation.Log;
import net.easyjava.tools.model.ModelBean;
import net.easyjava.tools.model.ModelConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by wenxiaoning on 15/12/18.
 */
@Controller
@RequestMapping("/api/user")
public class UserController extends BaseController {
    private  Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    @Resource
    private UserManager userManager;


    /**
     * 同一个浏览器用户第一次访问页面的时候返回一个随机用户
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/{type}")
    @ResponseBody
    public ModelBean getUserByType(
            @PathVariable String type,
            //注册用参数
            @RequestParam(required = false,defaultValue = "") String name,
            @RequestParam(required = false,defaultValue = "") String password,

            HttpServletRequest request, HttpServletResponse response) {
        ModelBean modelBean = null;
        String message = ModelConstants.MESSAGE_ERROR;
        try {

            if(WebConstants.USER_COOKIE.equals(type)){
                //从cookie中获取用户
                modelBean = userManager.userCookie(request, response);
            }else if(WebConstants.USER_LOGIN.equals(type)){
                //登陆
            }else if(WebConstants.USER_REGISTER.equals(type)){
                //注册
            }else{
                message = "地址有误";
                throw new Exception(message);
            }

        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            modelBean = new ModelBean(message);
        }


        return model(modelBean, request, response);
    }
}
